Skip to main content
Version: v5.0

group

$group#

Separates documents into groups according to a group key you define and outputs one document for each unique group key. You can also use accumalator operators to compute data for each group.

A group key can be an existing field, a group of fields, or the result of an expression. Use the _id field in the $group pipeline stage to set the group key.

Note: The $group method does not order its output documents - instead, you can sort documents with a $sort stage earlier in the pipeline.

Syntax#

{  "$group":    {      "_id": "<expression>", // Group key      "<customField1>": { "<accumulator1>" : "<expression1>" },    } }
FieldTypeDescriptionRequired
_idexpressionDefine an expression for the group key.Required
Custom fieldDefine a custom field, then define an accumulator operator and expression pair to compute the data you want for the custom field. You can define multiple custom fields.Optional

Accumulator operators#

Accumulator operatorReturnsNotes
$addToSetAn array of unique expression values for each groupOrder of the array elements is undefined
$avgAn average of numerical valuesIgnores non-numeric values
$countThe number of documents in a group
$firstA value from the first document for each groupFor order, sort documents with a $sort stage earlier in the pipeline.
$lastA value from the last document for each groupFor order, sort documents with a $sort stage earlier in the pipeline.
$maxThe highest expression value for each group
$minThe lowest expression value for each group
$pushAn array of expression values for documents in each group
$stdDevPopThe population standard deviation of the input values
$sumA sum of numerical valuesIgnores non-numeric values

Example#

In the following example, in the $group stage of the pipeline, the group key is defined by telemetry sensor source id. The custom fields are defined as follows:

  • "count": Counts the documents for each group
  • "avgtemp": Computes the average of the temp values for each group
  • "low": Gets the temp value of the first document in the group using the $first operator. The documents are sorted by temp in ascending order in a previous stage.
  • "high": Gets the temp value of the last document in the group using the $last operator. The documents are sorted by temp in ascending order in a previous stage.

Sample Request#

[    {        "$match": {            "temp": {                "$exists": true            },            "_tsMetadata._sourceId": {                "$exists": true            }        }    },    {        "$sort": {            "temp": 1        }    },    {        "$group": {            "_id": "$_tsMetadata._sourceId",            "count": {                "$count": {}            },            "avgtemp": {                "$avg": "$temp"            },            "low": {                "$first": "$temp"            },            "high": {                "$last": "$temp"            }        }    }]

Sample Response#

{    "_list": [        {            "high": 63,            "low": 63,            "count": 1,            "_id": "b1da8f7b-d6fd-46e0-a7f1-69d2118c05c2",            "avgtemp": 63        },        {            "high": 65,            "low": 38,            "count": 3,            "_id": "7d4a1c16-c2e4-4159-a5b8-5814f4bfe6e2",            "avgtemp": 55.333333333333336        },        {            "high": 83,            "low": 43,            "count": 3,            "_id": "ef4b5a5d-a5dd-48b0-b9c1-3618552cfe09",            "avgtemp": 61.333333333333336        },        {            "high": 85,            "low": 52,            "count": 3,            "_id": "ed11cc38-97c8-486e-b21e-2cccb7271173",            "avgtemp": 72.33333333333333        }    ]}